package com.android.wake_up_enable_hotspot;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.core.app.ActivityCompat;

import com.socks.library.KLog;

import java.util.Calendar;

public class BootCompleteService extends Service {
    private static final String KEY_LAST_MAC = "last_mac";
    private static final String KEY_LAST_SSID = "last_ssid";
    private BluetoothAdapter mBluetoothAdapter;
    private final BroadcastReceiver receiver = new BroadcastReceiver() { // from class: com.baidu.unlock_open_hotspot.BootCompleteService.1
        /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            char c;
            String action = intent.getAction();
            if (TextUtils.isEmpty(action)) {
                return;
            }
            switch (action.hashCode()) {
                case -2128145023:
                    if (action.equals("android.intent.action.SCREEN_OFF")) {
                        c = 2;
                        break;
                    }
                    c = 65535;
                    break;
                case -1454123155:
                    if (action.equals("android.intent.action.SCREEN_ON")) {
                        c = 1;
                        break;
                    }
                    c = 65535;
                    break;
                case 823795052:
                    if (action.equals("android.intent.action.USER_PRESENT")) {
                        c = 3;
                        break;
                    }
                    c = 65535;
                    break;
                case 1167529923:
                    if (action.equals("android.bluetooth.device.action.FOUND")) {
                        c = 0;
                        break;
                    }
                    c = 65535;
                    break;
                default:
                    c = 65535;
                    break;
            }
            switch (c) {
                case 0:
                    BluetoothDevice scanDevice = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    if (scanDevice == null) {
                        return;
                    }
                    ActivityCompat.checkSelfPermission(BootCompleteService.this, "android.permission.BLUETOOTH_CONNECT");
                    String deviceName = scanDevice.getName();
                    String deviceAddress = scanDevice.getAddress();
                    if (deviceName == null) {
                        return;
                    }
                    KLog.d("搜索到了: name=" + deviceName + "  address=" + deviceAddress);
                    if (TextUtils.equals(BootCompleteService.this.target_device_address, deviceAddress)) {
                        boolean ret = BootCompleteService.this.mBluetoothAdapter.cancelDiscovery();
                        KLog.e("取消蓝牙搜索操作 " + ret);
                        KLog.e("找到" + BootCompleteService.this.target_device_name + ":" + BootCompleteService.this.target_device_address + "，执行打开热点操作");
                        if (SpUtil.isFeaturesEnable(context)) {
                            BootCompleteService.this.requestSystemSetPermissionAndOptionWifiAp();
                            return;
                        }
                        return;
                    }
                    return;
                case 1:
                    KLog.e("屏幕已开启");
                    if (WifiApUtil.isWifiApOpen(BootCompleteService.this)) {
                        KLog.e("热点已开启，无需再次扫描周边蓝牙设备");
                        return;
                    }
                    if (!SpUtil.isFeaturesEnable(BootCompleteService.this)) {
                        KLog.e("亮屏热点功能未启用，不执行蓝牙扫描动作");
                        return;
                    }
                    Calendar calendar = Calendar.getInstance();
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    System.out.println();
                    boolean timeEnable = false;
                    if (hour >= 17 && hour <= 19) {
                        timeEnable = true;
                    } else if (hour >= 7 && hour <= 9) {
                        timeEnable = true;
                    }
                    if (!timeEnable) {
                        KLog.e("未在上下班时间，不执行蓝牙扫描操作，减少电量消耗");
                        return;
                    }
                    boolean ret2 = BootCompleteService.this.mBluetoothAdapter.startDiscovery();
                    KLog.e("startDiscovery ret = " + ret2);
                    return;
                case 2:
                    KLog.e("屏幕已熄灭");
                    return;
                case 3:
                    KLog.e("屏幕已解锁");
                    return;
                default:
                    return;
            }
        }
    };
    private String target_device_address;
    private String target_device_name;

    @Override // android.app.Service
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override // android.app.Service
    public void onCreate() {
        super.onCreate();
        KLog.e();
        this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        this.target_device_name = preferences.getString(KEY_LAST_SSID, null);
        this.target_device_address = preferences.getString(KEY_LAST_MAC, null);
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.SCREEN_ON");
        filter.addAction("android.intent.action.SCREEN_OFF");
        filter.addAction("android.intent.action.USER_PRESENT");
        filter.addAction("android.bluetooth.device.action.FOUND");
        registerReceiver(this.receiver, filter);
    }

    @Override // android.app.Service
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification.Builder builder;
        KLog.e();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = getPackageName();
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
            builder = new Notification.Builder(this, channelId);
        } else {
            builder = new Notification.Builder(this);
        }
        Notification notification = builder
//                .setContentTitle("contentTitle")
                .setContentTitle(getString(R.string.app_name))
//                .setTicker("ticker")
//                .setSubText("subText")
//                .setContentText("contentText")
                .setContentText("亮屏操作正在监控中")
                .setSmallIcon(R.mipmap.ic_launcher)
                .build();
        startForeground(1, notification);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override // android.app.Service
    public void onDestroy() {
        super.onDestroy();
        KLog.e();
        unregisterReceiver(this.receiver);
    }

    public void requestSystemSetPermissionAndOptionWifiAp() {
        if (!Settings.System.canWrite(this)) {
            KLog.e("没有授予相关权限，部分功能将受影响");
        } else {
            WifiApUtil.setWifiApEnabled(true);
        }
    }

}